<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>订单管理 - Express</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">

    <link rel="stylesheet" th:href="@{/public/assets/css/bootstrap.min.css}" href="../../public/assets/css/bootstrap.min.css"/>
    <link rel="stylesheet" th:href="@{/public/assets/css/bootstrap-table.css}" href="../../public/assets/css/bootstrap-table.css"/>
    <link rel="stylesheet" th:href="@{/public/assets/css/express.css}" href="../../public/assets/css/express.css"/>
    <link rel="stylesheet" th:href="@{/public/assets/css/star-rating.min.css}" href="../../public/assets/css/star-rating.min.css"/>
</head>
<body>
    <div class="container-fluid">
        <!-- 头部 -->
        <nav th:replace="common/nav::html"></nav>
        <!-- 主体 -->
        <div class="row">
            <!-- 左侧菜单 -->
            <div th:replace="common/sidebar::html"></div>
            <div class="col-md-10">
                <!-- 筛选条件 -->
                <div class="panel-heading">
                    <form class="form-inline">
                        <div class="form-group">
                            <label for="orderStatus">订单状态</label>
                            <select id="orderStatus" class="form-control">
                                <option value=-1>所有状态</option>
                                <option value=1>等待接单</option>
                                <option value=2>派送中</option>
                                <option value=3>订单完成</option>
                                <option value=4>订单异常</option>
                                <option value=5>订单取消</option>
                                <option value=6>等待揽收</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="inputStartDate" class="ml10">起</label>
                            <input type="date" id="inputStartDate" class="form-control ml10">
                            <label for="inputEndDate" class="ml10">讫</label>
                            <input type="date" id="inputEndDate" class="form-control ml10">
                        </div>
                        <div class="form-group">
                            <label for="inputId">订单号</label>
                            <input type="text" class="form-control" id="inputId">
                        </div>
                        <button type="button" class="btn btn-info ml10" onclick="flushTable()">条件查询</button>
                    </form>
                </div>
                <div class="panel-body">
                    <div id="toolbar" class="btn-group">
                        <button id="btn_cancel" type="button" class="btn btn-warning" onclick="reDistributionCourier()">
                            <span class="glyphicon glyphicon-repeat" aria-hidden="true"></span> 重新分配
                        </button>
                    </div>
                    <div class="alert alert-info alert-dismissible fade in" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                        <strong>完成订单：</strong> 订单状态需为 【派送中】 或 【订单异常】
                        <strong>异常订单：</strong> 订单状态需为 【派送中】
                        <strong>订单评价：</strong> 订单状态需为 【订单完成】 或 【订单异常】
                        <strong>订单取消：</strong> 创建订单后10分钟以内不支付
                    </div>
                    <table id="tb_history"></table>
                </div>
            </div>
        </div>
    </div>

    <!-- 订单详情模态框 -->
    <div class="modal fade" id="infoModel" tabindex="-1" role="dialog" aria-labelledby="infoModelLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="infoModelLabel">订单详情</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label for="modOrderId" class="col-sm-2 control-label">订单号</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modOrderId"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modServerType" class="col-sm-2 control-label">服务类型</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modServerType"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modSchoolName" class="col-sm-2 control-label">学校信息</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modSchoolName"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modOdd" class="col-sm-2 control-label">快递单号</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modOdd"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modRecName" class="col-sm-2 control-label">收件姓名</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modRecName"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modRecTel" class="col-sm-2 control-label">收件电话</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modRecTel"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modRecAddress" class="col-sm-2 control-label">收件地址</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modRecAddress"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modSenderName" class="col-sm-2 control-label">寄件姓名</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modSenderName"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modSenderTel" class="col-sm-2 control-label">寄件电话</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modSenderTel"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modSenderAddress" class="col-sm-2 control-label">寄件地址</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modSenderAddress"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modCompanyName" class="col-sm-2 control-label">快递公司</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modCompanyName"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modUserRemark" class="col-sm-2 control-label">用户备注</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modUserRemark"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modPaymentType" class="col-sm-2 control-label">支付方式</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modPaymentType"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modPaymentStatus" class="col-sm-2 control-label">支付状态</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modPaymentStatus"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modOrderStatus" class="col-sm-2 control-label">订单状态</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modOrderStatus"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modPayment" class="col-sm-2 control-label">支付金额</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modPayment"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modCourierName" class="col-sm-2 control-label">快递员姓名</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modCourierName"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modCourierTel" class="col-sm-2 control-label">快递员电话</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modCourierTel"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="modCourierRemark" class="col-sm-2 control-label">快递员备注</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" id="modCourierRemark"></p>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>


    <script th:src="@{/public/assets/js/http.js}" src="../../public/assets/js/http.js"></script>
    <script th:src="@{/public/assets/js/express.js}" src="../../public/assets/js/express.js"></script>
    <script th:src="@{/public/assets/js/jquery.min.js}" src="../../public/assets/js/jquery.min.js"></script>
    <script th:src="@{/public/assets/js/jquery.validate.min.js}" src="../../public/assets/js/jquery.validate.min.js"></script>
    <script th:src="@{/public/assets/layer/layer.js}" src="../../public/assets/layer/layer.js"></script>
    <script th:src="@{/public/assets/js/bootstrap.min.js}" src="../../public/assets/js/bootstrap.min.js"></script>
    <script th:src="@{/public/assets/js/bootstrap-table.min.js}" src="../../public/assets/js/bootstrap-table.min.js"></script>
    <script th:src="@{/public/assets/js/bootstrap-table-export.min.js}" src="../../public/assets/js/bootstrap-table-export.min.js"></script>
    <script th:src="@{/public/assets/js/bootstrap-table-zh-CN.min.js}" src="../../public/assets/js/bootstrap-table-zh-CN.min.js"></script>
    <script th:src="@{/public/assets/js/tableExport.min.js}" src="../../public/assets/js/tableExport.min.js"></script>
    <script th:src="@{/public/assets/js/star-rating.min.js}" src="../../public/assets/js/star-rating.min.js"></script>
    <!-- 公用头部JS start -->
    <div th:replace="~{common/configJS :: configJS}">...</div>
    <!-- 公用头部JS end -->
    <script>
        let orderListUrl = $('#orderListUrl').val();
        let orderDetailUrl = $('#orderDetailUrl').val();
        let pickUpOrderUrl = $('#pickUpOrderUrl').val();
        let finishOrderUrl = $('#finishOrderUrl').val();
        let reDistributionCourierUrl = $('#reDistributionCourierUrl').val();
        let uid = getCookieValue('uid');
        let token = "Bearer " + uid;
        // alert(token);
        $(function () {
            $("#orderManager-sidebar").addClass('active');

            //1、初始化Table
            var oTable = new TableInit();
            oTable.Init();

            $("#inputScore").rating({
                showClear: false,
                starCaptions: function (val) {
                    return val;
                },
                starCaptionClasses: function (val) {
                    if (val < 3) {
                        return 'label label-danger';
                    } else if(val < 7) {
                        return 'label label-warning';
                    } else {
                        return 'label label-success';
                    }
                },
                hoverOnClear: false
            });
        });

        var TableInit = function () {
            let oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function () {
                $('#tb_history').bootstrapTable({
                    ajaxOptions: {
                        headers: {
                            'Authorization':token
                        }
                    },
                    url: orderListUrl,          //请求后台的URL（*）
                    method: HTTP.GET,                   //请求方式（*）
                    toolbar: '#toolbar',                //工具按钮组id
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true,                   //是否显示分页（*）
                    sortable: false,                    //是否启用排序
                    sortOrder: "asc",                   //排序方式
                    queryParams: oTableInit.queryParams,//传递参数（*）
                    sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber:1,                       //初始化加载第一页，默认第一页
                    pageSize: 10,                       //每页的记录行数（*）
                    pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                    search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端。后台分页时意义不大
                    showColumns: true,                  //是否显示所有的列
                    showRefresh: true,                  //是否显示刷新按钮
                    minimumCountColumns: 2,             //最少允许的列数
                    clickToSelect: true,                //是否启用点击选中行
                    // height: 750,                     //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                    uniqueId: "id",                     //每一行的唯一标识，一般为主键列
                    showToggle:false,                   //是否显示详细视图和列表视图的切换按钮
                    cardView: false,                    //是否显示详细视图
                    detailView: false,                  //是否显示父子表
                    showExport: true,                   //是否显示导出
                    exportDataType: "basic",            //导出的模式是当前页basic、所有数据all、选中数据selected。
                    columns: [{
                        checkbox: true
                    }, {
                        field: 'id',
                        title: '订单号'
                    }, {
                        field: 'odd',
                        title: '快递单号'
                    }, {
                        field: 'company',
                        title: '快递公司'
                    }, {
                        field: 'recName',
                        title: '收件人'
                    }, {
                        field: 'recTel',
                        title: '收件电话'
                    }, {
                        field: 'recAddress',
                        title: '收件地址'
                    }, {
                        field: 'address',
                        title: '快递寄达地址'
                    }, {
                        field: 'remark',
                        title: '下单备注'
                    }, {
                        field: 'orderStatus',
                        title: '订单状态',
                        formatter: orderFormatter
                    }, {
                        field: 'createDate',
                        title: '下单时间'
                    }, {
                        field: 'id',
                        title: '操作',
                        width: 120,
                        formatter: actionFormatter
                    }]
                });
            };

            // 传递给后台的参数
            oTableInit.queryParams = function (params) {
                return {   //这里的键的名字和后台参数必须一致
                    size: params.limit,                             //页面大小
                    current: (params.offset / params.limit) + 1,   //页码
                    type: 0,
                    // 筛选参数
                    id: $("#inputId").val(),
                    orderStatus: $("#orderStatus").val(),
                    startDate: $("#inputStartDate").val(),
                    endDate:$("#inputEndDate").val(),
                };
            };
            return oTableInit;
        };

        //操作栏的格式化
        function actionFormatter(value, row, index) {
            let id = "'" + value + "'";
            let result = '<button class="btn btn-xs btn-info" onclick="showDetail('+id+')" title="查看"><span class="glyphicon glyphicon-search"></span></button>\n';
            // 等待揽收
            if (row.orderStatus === 6){
                result += '<button class="btn btn-xs btn-success" onclick="pickUpOrder('+id+')" title="揽收完成"><span class="glyphicon glyphicon-ok-circle"></span></button>\n' +
                    '         <button class="btn btn-xs btn-danger" onclick="errorComplete('+id+')" title="设为异常"><span class="glyphicon glyphicon-remove-circle"></span></button>\n';
            }
            // 等待接单
            if(row.orderStatus === 2) {
                result += '<button class="btn btn-xs btn-success" onclick="completeOrder('+id+')" title="订单完成"><span class="glyphicon glyphicon-ok-circle"></span></button>\n' +
                    '         <button class="btn btn-xs btn-danger" onclick="errorComplete('+id+')" title="设为异常"><span class="glyphicon glyphicon-remove-circle"></span></button>\n';
            }
            // 订单完成
            if(row.orderStatus === 3 && row.canScore === "1") {
                result += '<button class="btn btn-xs btn-default" onclick="showEvaluate('+id+')" title="评价"><span class="glyphicon glyphicon-edit"></span></button>\n';
            }
            return result;
        }
        // 订单状态的格式化
        function orderFormatter(value, row, index) {
            if(value === 4)
                return '<span style="color:red">订单异常</span>';
            else if (value === 3)
                return '<span style="color:olivedrab">订单完成</span>';
            else if (value === 2)
                return '<span style="color:#42afff">派送中</span>';
            else if (value === 1)
                return '<span style="color:grey">等待接单</span>';
            else if (value === 5)
                return '<span style="color:grey">订单取消</span>';
            else if (value === 6)
                return '<span style="color:grey">等待揽收</span>';
        }

        function flushTable() {
            $("#tb_history").bootstrapTable('refresh',{
                ajaxOptions: {
                    headers: {
                        'Authorization':token
                    }
                },
                url : orderListUrl
            } );
        }

        // 重新分配配送员
        function reDistributionCourier() {
            // debugger;
            let row = $('#tb_history').bootstrapTable("getSelections");
            if(row.length > 0) {
                let ids = new Array();
                for (let i = 0; i < row.length; i++) {
                    if (row[i].orderStatus === 2 || row[i].orderStatus === 6) {
                        ids.push(row[i].id);
                    }
                }
                if (ids.length === 0) {
                    layer.msg("选中项不包含可重新分配的订单(派送中/等待揽收)",{icon:5});
                    return;
                }else {
                    layer.confirm("重新分配订单号(配送员不是请假状态可能重新分配到他身上)：" + ids + "，确认执行？", {btn:["确认","取消"]},
                        function () {
                            sendArray(HTTP.POST, reDistributionCourierUrl, {"Authorization": token}, {"orderIds":ids}, false,
                                function (res) {
                                    if (res.code === 10000){
                                        layer.msg(res.data,{icon:6});
                                        flushTable();
                                    }else {
                                        layer.msg("操作失败！",{icon:5});
                                    }
                                },
                                function () {
                                    layer.msg("未知错误!", {icon:5});
                                })
                        });
                }
            }
        }

        function showDetail(id) {
            sendJson(HTTP.GET, orderDetailUrl + id, {"Authorization": token}, null, false, function (res) {
                if (res.code !== 10000) {
                    layer.msg(res.data,{icon:5});
                } else {
                    // 初始化模态框信息
                    let data = res.data;
                    $("#modOrderId").text(data.orderId);
                    $("#modServerType").text(data.serverType);
                    $("#modRecName").text(data.recName);
                    $("#modRecTel").text(data.recTel);
                    $("#modRecAddress").text(data.recAddress);
                    $("#modSenderName").text(data.senderName);
                    $("#modSenderTel").text(data.senderTel);
                    $("#modSenderAddress").text(data.senderAddress);
                    $("#modOdd").text(data.odd);
                    $("#modCompanyName").text(data.companyName);
                    $("#modUserRemark").text(data.userRemark);
                    $("#modPaymentType").text(data.paymentType);
                    $("#modPayment").text(data.payment);
                    $("#modPaymentStatus").text(data.paymentStatus);
                    $("#modOrderStatus").text(data.orderStatus);
                    $("#modCourierName").text(data.courierName);
                    $("#modCourierTel").text(data.courierTel);
                    $("#modCourierRemark").text(data.courierRemark);
                    $("#modSchoolName").text(data.schoolName);


                    $("#infoModel").modal("show");
                }
            }, function () {
                layer.msg("未知错误",{icon:2});
            });
        }

        function pickUpOrder(id) {
            layer.prompt({title:"揽收完成", formType:2},function(val, index) {
                sendJson(HTTP.POST, pickUpOrderUrl + id, {"Authorization": token}, {"courierRemark": val}, false, function (res) {
                        if (res.code === 10000){
                            layer.msg("揽收成功！", {icon: 6});
                            layer.close(index);
                            flushTable();
                        }else {
                            layer.msg(res.data, {icon: 5});
                            layer.close(index);
                        }
                    },
                    function () {
                        layer.msg("未知错误", {icon: 5});
                    })
            });
        }

        function completeOrder(id) {
            layer.prompt({title:"订单完成", formType:2},function(val, index) {
                sendJson(HTTP.POST, finishOrderUrl + id, {"Authorization": token}, {"courierRemark": val}, false, function (res) {
                    if (res.code === 10000) {
                        layer.msg("操作成功", {icon: 6});
                        layer.close(index);
                        flushTable();
                    } else {
                        layer.msg(res.data, {icon: 5});
                    }
                }, function () {
                    layer.msg("未知错误", {icon: 5});
                });
            });
        }


        function errorComplete(id) {
            layer.prompt({title:"订单异常", formType:2},function(val, index) {
                sendJson(HTTP.POST, $('#setOrderExceptionUrl').val() + id, {"Authorization": token}, {"courierRemark": val}, false, function (res) {
                    if (res.code === 10000) {
                        layer.msg("操作成功", {icon: 6});
                        layer.close(index);
                        flushTable();
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }, function () {
                    layer.msg("未知错误", {icon: 5});
                });
            });
        }


    </script>
</body>
</html>